import Taro from "@tarojs/taro";
import {host} from "@/_framework/Config";
import {failToast, infoToast} from "@/_framework/services/Taro";
import {ShowTypeEnum} from "@/_framework/Enum";
import {goAuthorize, goHome} from "@/_framework/services/Go";

const codesMap = {
    // 4xx 客户端错误
    400: '错误请求',
    401: '未授权',
    402: '需要付款',
    403: '禁止访问',
    404: '未找到',
    408: '请求超时',
    409: '令牌无效或已过期',
    423: '已锁定',
    425: '过早请求',
    429: '请求过多',
    451: '因法律原因不可用',
    460: '缺少客户端ID',

    // 5xx 服务端错误
    500: '服务器内部错误',
    501: '未实现',
    502: '网关错误',
    503: '服务不可用',
    504: '网关超时',
    505: '版本不支持',
    507: '存储空间不足',
    508: '检测到循环',
    509: '超出带宽限制',
    555: '未知错误'
};

export async function post(_url, params) {
    const access_token = Taro.getStorageSync("access_token");
    const data = JSON.stringify(params)
    const url = `${host()}/${_url}`
    const header = {
        "content-type": "application/json", Authorization: `Bearer ${access_token}`,
    }
    const response = await Taro.request({method: "POST", url, data, header,});
    return dealResponse(response)

}

export async function postAuth(params) {
    const base64AuthString = 'Y2hhb3MtY2xpZW50OnNlY3JldA==';
    const formData = new URLSearchParams(params);
    const url = `${host()}/auth/oauth2/token`;
    const data = formData.toString()
    const header = {
        "content-type": "application/x-www-form-urlencoded", "Authorization": `Basic ${base64AuthString}`
    }
    const response = await Taro.request({method: "POST", url, header, data});
    return dealResponse(response)

}

export async function dealResponse(response) {
    const {statusCode, data} = response

    if (statusCode !== 200) {
        const {msg, code = 555, showType} = data;
        let description;
        if (statusCode === 400) {
            description = msg || '错误请求';
        }
        if (statusCode === 401) {
            if (code >= 1000) {
                //认证错误场景
                description = msg || '认证错误';
            } else {
                description = '请先认证';
            }
            await goAuthorize()
        }
        if (statusCode === 403) {
            description = '无访问权限';
        }
        if (statusCode === 404) {
            description = '请求资源不存在';
        }
        if (statusCode === 409) {
            description = '令牌过期';
            await goHome()
        }
        if (statusCode === 500) {
            description = '服务异常，请稍后再试';
        }
        if (statusCode === 502) {
            description = '网关异常，请稍后再试';
        }
        if (statusCode === 503) {
            description = '服务不可用，请稍后再试';
        }
        if (statusCode === 504) {
            description = '网关超时，请稍后再试';
        }
        if (showType !== ShowTypeEnum.SILENT && code !== 401) {
            infoToast(description)
        }
        return
    }

    const {msg, code, showType, ...rest} = data;
    if (code > 200 && code <= 555) {
        if (showType !== ShowTypeEnum.SILENT) {
            const description = `${codesMap[code]}:${msg}` || '未知错误';
            failToast(description)
        }
    }

    return {...rest};
}
